package com.zq.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.zq.entity.User;
import com.zq.repository.UserRepository;
import com.zq.service.UserService;
import com.zq.utils.MD5;
import com.zq.utils.ResultInfoUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

/**
 * @Author:任人渐疏
 * @Date:Created in 11:41 上午 2020/6/26
 * Update:true
 */
@RestController
@RequestMapping("login")
public class LoginController {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserService userService;
    @PostMapping
    public Object login(String yonghuming,String password){
        Subject subject= SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(yonghuming, MD5.JiaM(password));
        try {
            subject.login(token);
            token.isRememberMe();
            return new ResultInfoUtil(0, "登陆成功", subject.getPrincipal());
        }catch (UnknownAccountException e) {
            return new ResultInfoUtil(-1, "用户名不正确！", null);
        }catch (IncorrectCredentialsException e) {
            return new ResultInfoUtil(-1, "密码不正确！", null);
        }
    }
}
